package com.htthorizon.neo4j.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.htthorizon.common.core.domain.R;
import com.htthorizon.neo4j.dto.*;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/neo4j/data")
@Slf4j
public class DataController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private HostConfig hostConfig;

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Brilliant\\Desktop\\新建文件夹 (3)");
       file.mkdirs();
    }

    @GetMapping("/bd")
    public R data() throws JSchException, IOException {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select  '审计自建模型'  as root ,a1.dict_name    as relation,a2.dict_label  as  bd from sys_dict_type  a1  inner join sys_dict_data a2 on  a2.dict_type=a1.dict_type where a1.dict_type  ='speciality_code'");
       log.info("获取数据成功");
        List<Bd> list=new ArrayList<>(maps.size());
        for (Map<String,Object> map:maps){
            Bd bd = new Bd();
            bd.setRoot(map.get("root").toString());
            bd.setRelation(map.get("relation").toString());
            bd.setBd(map.get("bd").toString());
            list.add(bd);
        }
        String path= hostConfig.getPath()+"\\bd.csv";
        File file1 = new File(hostConfig.getPath());
        if (!file1.exists()){
            file1.mkdirs();
        }
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
            log.info("不存在文件夹创建文件夹");
        }
        ExcelWriter excelWriter = EasyExcelFactory.write(file).excelType(ExcelTypeEnum.CSV).charset(Charset.forName("GBK"))
                .build();
        List<List<String>> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        List<String> list4 = new ArrayList<>();
        list2.add("root");
        list3.add("relation");
        list4.add("bd");
        list1.add(list2);
        list1.add(list3);
        list1.add(list4);
        WriteSheet data = EasyExcelFactory.writerSheet("data").head(list1).build();
        excelWriter.write(list,data);
        excelWriter.finish();

        log.info("数据生产csv成功");

        String user = hostConfig.getUsername();
        String host = hostConfig.getHost();
        String rfile = hostConfig.getPathremote();
        String lfile = hostConfig.getPath()+"\\bd.csv";
        log.info(user);
        log.info(host);
        log.info(rfile);
        log.info(lfile);
        log.info(hostConfig.getPassword());

        JSch jsch = new JSch();
        Session session = null;
        try {
            // 建立SSH会话
            session = jsch.getSession(user, host, 10022);
            // 连接认证
            session.setPassword(hostConfig.getPassword());
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // 打开一个SFTP通道
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;

            // 进行文件传输
            sftp.put(new FileInputStream(lfile), rfile );

            // 关闭连接
            channel.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException | IOException e) {
            e.printStackTrace();
        }


        return R.ok("成功");
    }


}
